//
//  WSLoginViewController.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/22.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSLoginViewController.h"
#import "WSRegisterViewController.h"
#import "WSTabBarController.h"
#import "ZhStaffTabBarViewController.h"

#import "LoginService.h"
#import "UploadService.h"
#import "WSModuleManager.h"

@interface WSLoginViewController ()<UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *logoImage;
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *passView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *serviceBtn;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;

@end

@implementation WSLoginViewController

#pragma mark - Life cycle
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setup];
}

// 初始化
- (void)setup {
    // 设置底部边框
    [self.phoneView setBorder:kSeparatorColor borderWidth:1 borderType:UIBorderSideTypeBottom];
    [self.passView setBorder:kSeparatorColor borderWidth:1 borderType:UIBorderSideTypeBottom];
    
    NSMutableAttributedString *mAttributeStr = [[NSMutableAttributedString alloc] initWithString:_registBtn.currentTitle];
    
    // 设置下划线
    [mAttributeStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:(NSRange){0, mAttributeStr.length}];
    // 设置下划线颜色
    [mAttributeStr addAttribute:NSForegroundColorAttributeName value:COLOR_HEX(0x3BC7B1) range:NSMakeRange(0, mAttributeStr.length)];
    [self.registBtn setAttributedTitle:mAttributeStr forState:UIControlStateNormal];
    
    [self.tipLabel addAttributeText:_tipLabel.text attrs:@{NSForegroundColorAttributeName: kThemeGreenColor} range:NSMakeRange(6, 8)];
}


#pragma mark - Button Actions

// 注册
- (IBAction)registBtnClick:(id)sender {
    [self performSegueWithIdentifier:@"RegistVc" sender:nil];
}

// 登录
- (IBAction)loginBtnClick:(id)sender {
    if (![_phoneTextField hasText]) {
        [self showHint:@"请输入手机号"];
        return;
    }
    if (![_pwdTextField hasText]) {
        [self showHint:@"请输入密码"];
        return;
    }
    if (!_serviceBtn.isSelected) {
        [self showHint:@"请先阅读并同意协议"];
        return;
    }
    
    [LoginService loginWithAccout:_phoneTextField.text password:_pwdTextField.text complete:^(BOOL success) {
        if (success) {
            [WSModuleManager showHomeMoudle];
            /// 获取七牛上传token
            [UploadService getQnUploadInfoWithType:UploadTypeImage params:nil completion:^(id  _Nonnull result) {
                ResponseModel *response = [ResponseModel yy_modelWithJSON:result];
                if (!response.data) {
                    return ;
                }
                [TFUserDefault setValue:response.data[@"token"] forKey:@"QNToken"];
                [TFUserDefault setValue:response.data[@"url"] forKey:@"imageUrl"];

            }];
        }
    }];
}

// 阅读同意用户服务协议
- (IBAction)selecBtnClick:(UIButton *)sender {
    sender.selected = !sender.isSelected;
}

// 设置根控制器
- (void)setRootViewController:(UIViewController *)vc {
    typedef void (^Animation)(void);
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        KAppWindow.rootViewController = vc;
        [UIView setAnimationsEnabled:oldState];
    };
    [UIView transitionWithView:KAppWindow
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:animation
                    completion:nil];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"RegistVc"]) { // 用户注册
        
    } else if ([segue.identifier isEqualToString:@"ForgetPwd"]) { // 忘记密码
        WSRegisterViewController *targeVc = segue.destinationViewController;
        targeVc.isForgetPwd = YES;
        targeVc.title = @"忘记密码";
    }
}


@end
